/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * comprarCasaHotel.java
 *
 * Created on 12/05/2011, 12:24:10 AM
 */
package logica;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jorge Maldonado
 */
public class comprarCasaHotel extends javax.swing.JDialog {

    /** Creates new form comprarCasaHotel */
    private DefaultTableModel mod;
    private ArrayList<usuario> usuarios;

    public comprarCasaHotel(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public comprarCasaHotel(ArrayList<usuario> usuarios, propiedad[] prop) {
        this(null, true);
        this.usuarios = usuarios;
        mod = new DefaultTableModel(new Object[]{"Propiedad", "Numero de casas"}, 0);
        for (int i = 0; i < usuarios.size(); i++) {
            jComboBox1.addItem(usuarios.get(i).getNombre());
        }
        llenarTabla(usuarios.get(0).amarillo, usuario.CANTIDAD_AMARILLO,0,Color.YELLOW);
        llenarTabla(usuarios.get(0).azul, usuario.CANTIDAD_AZUL,0,Color.BLUE);
        llenarTabla(usuarios.get(0).cian, usuario.CANTIDAD_CIAN,0,Color.CYAN);
        llenarTabla(usuarios.get(0).morado, usuario.CANTIDAD_MORADO,0,Color.MAGENTA);
        llenarTabla(usuarios.get(0).naranja, usuario.CANTIDAD_NARANJA,0,Color.ORANGE);
        llenarTabla(usuarios.get(0).rojo, usuario.CANTIDAD_ROJO,0,Color.RED);
        llenarTabla(usuarios.get(0).verde, usuario.CANTIDAD_VERDE,0,Color.GREEN);
        llenarTabla(usuarios.get(0).violeta, usuario.CANTIDAD_VIOLETA,0,Color.PINK);
        this.jTable1.setModel(mod);
    }

    private void llenarTabla(int color, int color2,int indice,Color color3) {
        int cantidadCasas = 3;
        if (color == color2) {
            for (int j = 0; j < usuarios.get(indice).getPropiedades().size(); j++) {
                if (usuarios.get(indice).getPropiedades().get(j).tipo == propiedad.TIPO_LOTE && ((lote) usuarios.get(indice).getPropiedades().get(j)).getColor() == color3) {
                    cantidadCasas = cantidadCasas < ((lote) usuarios.get(indice).getPropiedades().get(j)).getNumCasas() ? cantidadCasas : ((lote) usuarios.get(indice).getPropiedades().get(j)).getNumCasas();
                }
            }
            for (int j = 0; j < usuarios.get(indice).getPropiedades().size(); j++) {
                if (usuarios.get(indice).getPropiedades().get(j).tipo == propiedad.TIPO_LOTE && ((lote) usuarios.get(indice).getPropiedades().get(j)).getColor() == color3) {
                    if (cantidadCasas == ((lote) usuarios.get(indice).getPropiedades().get(j)).getNumCasas()) {
                        mod.addRow(new Object[]{usuarios.get(indice).getPropiedades().get(j).getDescripcion(), ((lote) usuarios.get(indice).getPropiedades().get(j)).getNumCasas()});
                    }
                }
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Usuario");

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Tipo");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Casa", "Hotel" }));
        jComboBox2.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox2ItemStateChanged(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Comprar");

        jButton2.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox1, 0, 171, Short.MAX_VALUE))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 283, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addGap(32, 32, 32))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox2ItemStateChanged
        // TODO add your handling code here:

    }//GEN-LAST:event_jComboBox2ItemStateChanged

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        mod = new DefaultTableModel(new Object[]{"Propiedad", "Numero de casas"}, 0);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).amarillo, usuario.CANTIDAD_AMARILLO,jComboBox2.getSelectedIndex(),Color.YELLOW);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).azul, usuario.CANTIDAD_AZUL,jComboBox2.getSelectedIndex(),Color.BLUE);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).cian, usuario.CANTIDAD_CIAN,jComboBox2.getSelectedIndex(),Color.CYAN);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).morado, usuario.CANTIDAD_MORADO,jComboBox2.getSelectedIndex(),Color.MAGENTA);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).naranja, usuario.CANTIDAD_NARANJA,jComboBox2.getSelectedIndex(),Color.ORANGE);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).rojo, usuario.CANTIDAD_ROJO,jComboBox2.getSelectedIndex(),Color.RED);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).verde, usuario.CANTIDAD_VERDE,jComboBox2.getSelectedIndex(),Color.GREEN);
        llenarTabla(usuarios.get(jComboBox2.getSelectedIndex()).violeta, usuario.CANTIDAD_VIOLETA,jComboBox2.getSelectedIndex(),Color.PINK);
        this.jTable1.setModel(mod);
    }//GEN-LAST:event_jComboBox1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
